package com.zdlyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdlyh.domain.ResponseResult;
import com.zdlyh.domain.dto.RoleDto;
import com.zdlyh.domain.entity.Role;
import com.zdlyh.domain.entity.RoleMenu;
import com.zdlyh.domain.entity.User;
import com.zdlyh.service.RoleMenuService;
import com.zdlyh.service.RoleService;
import com.zdlyh.mapper.RoleMapper;
import com.zdlyh.service.UserService;
import com.zdlyh.utils.BeanCopyUtils;
import com.zdlyh.domain.vo.PageVo;
import com.zdlyh.domain.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{
    @Autowired
    private UserService userService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<String> getRoleKeyByUserId(Long userId) {
        // 判断是否为管理员，不是则返回所具有的角色信息
        User user = userService.getById(userId);
        if (user.getType().equals("1")){
            List<String> list = new ArrayList<>();
            list.add("admin");
            return list;
        }
        return baseMapper.getRoleKeyByUserId(userId);
    }

    @Override
    public ResponseResult<PageVo> getRoleList(Integer pageNum, Integer pageSize, RoleDto roleDto) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        String roleName = roleDto.getRoleName();
        String status = roleDto.getStatus();
        queryWrapper.like(StringUtils.hasText(roleName),Role::getRoleName,roleName)
                .eq(StringUtils.hasText(status),Role::getStatus,status)
                .orderByAsc(Role::getRoleSort);
        Page<Role> page = new Page<>(pageNum,pageSize);
        List<Role> roleList = baseMapper.selectPage(page, queryWrapper).getRecords();

        List<RoleVo> roleVos = BeanCopyUtils.copyBeanList(roleList, RoleVo.class);

        PageVo pageVo = new PageVo(roleVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult changeStatus(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        baseMapper.updateById(role);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult addRole(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        baseMapper.insert(role);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRoleById(Long id) {
        Role role = baseMapper.selectById(id);
        RoleVo roleVo = BeanCopyUtils.copyBean(role, RoleVo.class);
        return ResponseResult.okResult(roleVo);
    }

    @Override
    public ResponseResult updateRole(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        // 改变角色的基本信息
        baseMapper.updateById(role);
        // 删除之前的角色权限
        roleMenuService.deleteRoleMenuByRoleId(roleDto.getId());
        // 新增现在的角色权限
        List<RoleMenu> roleMenus = Arrays.stream(roleDto.getMenuIds())
                .map(menuId -> new RoleMenu(roleDto.getId(), menuId))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenus);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteRoleById(Long id) {
        baseMapper.deleteById(id);
        roleMenuService.deleteRoleMenuByRoleId(id);
        return ResponseResult.okResult();
    }

    @Override
    public List<Role> getAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus,0);
        return baseMapper.selectList(queryWrapper);
    }
}




